<?php
/**
 * 基础类
 * $Id$
 */

if (!defined('IN_APP')) {
	die('Hacking attempt');
}

class APP {
	private $db_name = '';
	private $prefix = 'sp_';

	/**
	 * 够早函数
	 */
	public function __construct($db_name, $prefix) {
		$this->db_name = $db_name;
		$this->prefix = $prefix;
	}

	/**
	 * 给表添加前缀
	 */
	public function table($str) {
		return '`' . $this->db_name . '`.' . $this->prefix . $str . '`';
	}

	/**
	 * 密码编译方法
	 */
	public function compile_password($pass) {
		return md5($pass);
	}

	/**
	 * 取得当前域名 协议
	 */
	public function get_domain() {
		/* 协议*/
		$protocol = $this->http();
		/* 域名或IP地址*/
		if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
			$host = $_SERVER['HTTP_X_FORWARDED_HOST'];
		} elseif (isset($_SERVER['HTTP_HOST'])) {
			$host = $_SERVER['HTTP_HOST'];
		} else {
			/* 端口*/
			if (isset($_SERVER['SERVER_PORT'])) {
				$port = ':' . $_SERVER['SERVER_PORT'];
				
				if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol)) {
					$port = '';
				}
			} else {
				$port = ''
			}

			if (isset($_SERVER['SERVER_NAME'])) {
				$host = $_SERVER['SERVER_NAME'] . $port;
			} elseif (isset($_SERVER['SERVER_ADDR'])) {
				$host = $_SERVER['SERVER_ADDR'] . $port;
			}
		}
		return $protocol . $host;
	}

	/**
	 * 获得当前的URL地址
	 */
	public function url() {
		return $this->domain() . $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
	}

	/**
	 * http OR https
	 */
	public function http() {
		if (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS'] != 'off')) {
			return 'https://';
		}
		return 'http://';
	}
}
